package org.angmarc.app.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import org.angmarc.app.ResourceTable;
import org.angmarc.app.ToastUtils;
import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;
import org.angmarch.views.SpinnerTextFormatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/*
 * Copyright (C) 2021 hihope
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
    }

    @Override
    public void onActive() {
        super.onActive();
        setupDefault();
        setupTintedWithCustomClass();
        setupStrArray();
    }

    private void setupDefault() {
        NiceSpinner spinner = (NiceSpinner) findComponentById(ResourceTable.Id_nice_spinner);
        List<String> dataset = new LinkedList<>(Arrays.asList("One", "Two", "Three", "Four", "Five"));
        spinner.setArrowDrawable(ResourceTable.Media_ic_arrow);
        spinner.attachDataSource(dataset);
        spinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, Component view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();
                ToastUtils.showToast(MainAbilitySlice.this, "Selected: " + item, 0);
            }
        });
    }

    private void setupTintedWithCustomClass() {
        final NiceSpinner spinner =
                (NiceSpinner) findComponentById(ResourceTable.Id_tinted_nice_spinner);
        List<Person> people = new ArrayList<>();

        people.add(new Person("Tony", "Stark"));
        people.add(new Person("Steve", "Rogers"));
        people.add(new Person("Bruce", "Banner"));

        SpinnerTextFormatter textFormatter = new SpinnerTextFormatter<Person>() {
            @Override
            public String format(Person person) {
                return person.getName() + " " + person.getSurname();
            }
        };

        spinner.setSpinnerTextFormatter(textFormatter);
        spinner.setSelectedTextFormatter(textFormatter);
        spinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, Component view, int position, long id) {
                Person person = (Person) spinner.getSelectedItem();
                ToastUtils.showToast(MainAbilitySlice.this, "Selected: " + person.toString(), 0);
            }
        });
        spinner.attachDataSource(people);
    }

    private void setupStrArray() {
        NiceSpinner spinner = (NiceSpinner) findComponentById(ResourceTable.Id_niceSpinnerJson);
        spinner.setStrArrayData(ResourceTable.Strarray_courses);
        spinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, Component view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();
                ToastUtils.showToast(MainAbilitySlice.this, "Selected: " + item, 0);
            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

class Person {

    private String name;
    private String surname;

    Person(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    String getName() {
        return name;
    }

    String getSurname() {
        return surname;
    }

    @Override
    public String toString() {
        return name + " " + surname;
    }
}
